<?php

namespace App\Http\Middleware;

use App\service\TokenService;
use Closure;
use Illuminate\Http\Request;

class ApiMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        //第一步先取token
        $token=$request->header('token');
        //jwt 进行校验token
        $res=(new TokenService())->chekToken($token);

        if ($res['code']!=1){
            return json_encode(['error_code'=>999,'msg'=>$res['msg'],'data'=>''],400);
        }

        $request->uid=$res['data']->uid;

        //如果没有停止则向后执行
        return $next($request);
    }
}
